m=int(input())
class node:
    def __init__(self,index,tag):
        self.index=index
        self.tag=tag
    def __lt__(self,n):
        if self.index<n.index:
            return True
        elif self.index>n.index:
            return False
        else:
            return self.tag>n.tag

ns=[]

for i in range(0,m):
    index,tag=map(int,input().split())
    ns.append(node(index,tag))

ns.sort()

#预测挂科对
a=[0 for i in range(0,m)]
#预测未挂科对
b=[0 for i in range(0,m)]

for i in range(0,m):
    if ns[i].tag==1:
        if i>0:
            a[i]=a[i-1]
    else:
        if i==0:
            a[i]=1
        else:
            a[i]=a[i-1]+1

for i in range(m-1,-1,-1):
    if ns[i].tag==0:
        if i<m-1:
            b[i]=b[i+1]
    else:
        if i==m-1:
            b[i]=1
        else:
            b[i]=b[i+1]+1
    
result=0
count=0
for i in range(0,m):
    c=a[i]+b[i]
    if c>count or c==count and ns[i].index>result:
        result=ns[i].index
        count=c

print(result)